from munch import DefaultMunch

def colored_text(txt,color=None):
    """
    彩色打印字体
    
    参数:
        txt: 打印文本
        color: 文本颜色
        
    返回:
        result: 彩色文本
    """
    if color == 'red':
        result = f"\033[31m{txt}\033[0m"
    elif color == 'green':
        result = f"\033[32m{txt}\033[0m"
    elif color == 'yellow':
        result = f"\033[33m{txt}\033[0m"
    elif color == 'blue':
        result = f"\033[34m{txt}\033[0m"
    elif color == 'violet':
        result = f"\033[35m{txt}\033[0m"
    elif color == 'cyan':
        result = f"\033[36m{txt}\033[0m"
    elif color == 'gray':
        result = f"\033[37m{txt}\033[0m"
    elif color == 'black':
        result = f"\033[30m{txt}\033[0m"
    else:
        result = txt
    return result


class ResponseService:
    def success(data ,msg='success'):
        return {'code': 200,'data': data,'msg': msg,'success':True}
    def error(data ,msg='error'):
        return {'code': 500,'data': data,'msg': msg,'error':True}
    def permission(data='未登录',msg="暂无权限访问"):
        return {'code': 401,'data': data,'msg': msg,'error':True}
    
    
def dict2object(data: dict):
    return DefaultMunch.fromDict(data)


def productListgroupbyKey(l: list,key :str):
    d,r = {},[]
    for item in l:
        if d.get(item.get(key)):
            d[item.get(key)].append(item)
        else:
            d[item.get(key)] = [item]
    for prop in d:
        foods = d[prop]
        _d = {"id":prop}
        _d['name'] = d[prop][0]['pm.name']
        for item in foods:
            del item['pm.id']
            del item['pm.name']
            del item['typeId']
        _d['foods'] = foods
        r.append(_d)
    return r


NEED_LOGIN_PATH = [
    '/store/getStore',
    '/product/getProductList',
    '/person/getOrder'
]